package com.caiwangdai.app.extension

import android.content.Context
import android.text.InputType
import android.text.TextUtils
import android.view.inputmethod.InputMethodManager
import android.widget.EditText


//扩展函数&扩展属性


//使光标指向末尾处
fun EditText.setSelectionEnd() {
    this.setSelection(this.text.length)
}

//获取编辑框的内容
fun EditText.getContent(): String = this.text.toString().trimMargin()

//删除编辑框的内容
fun EditText.clearContent() {
    setText("")
}


//查看是否有空的值
fun EditText.isEmpty(vararg editTexts: EditText): Boolean {
    val str = text.toString().trimMargin()
    if (TextUtils.isEmpty(str)) {
        return true
    }
    return false
}


//展示密码 InputTypes
fun EditText.showPassword() {
    inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
    setSelectionEnd()
}

//隐藏密码  InputType
fun EditText.hidePassword() {
    inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
    setSelectionEnd()
}

//是否显示密码
fun EditText.changePasswordStatus(isShowPwd: Boolean) {
    if (isShowPwd)
        showPassword()
    else
        hidePassword()
}

//隐藏键盘
fun EditText.hideKeyBoard() {
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

    if (inputMethodManager.isActive(this)) {
        clearFocus()
        inputMethodManager.hideSoftInputFromWindow(windowToken, 0) //强制隐藏键盘
    }
}